// pages/past/past.js
let app=getApp();
let searchlist=[]
Page({
   //页面的初始数据
    data: {
        eventlist:null,
        inputShowed: false,
        inputVal: "",
        showsearch:false,
        openid:null,
        photo:"../image/memory.jpg",
        eventlength:1
    },
    // 生命周期函数--监听页面加载
    onLoad: function (options) {
      this.getevent();
      this.setData({
            search: this.search.bind(this)
        })
    },
    search: function (value) {
      let that=this;
      if(value!="")
      {
        return new Promise((resolve, reject) => {
          let slist=that.data.eventlist;
          var len = slist.length;
          var arr = [];
          let ob={
            text:'',
            value:0
          }
          searchlist=[]
          for(var i=0,j=1;i<len;i++){
              //如果字符串中不包含目标字符会返回-1
              var objeci=slist[i];
              if(objeci.eventtitle.indexOf(value)>=0){
                ob.text=objeci.eventtitle
                ob.value=j
                j=j+1;
                searchlist.push(objeci)
                arr.push(ob);
              }
              else if(objeci.eventarticle.indexOf(value)>=0 )
              {
                ob.text=objeci.eventarticle
                ob.value=j
                j=j+1;
                searchlist.push(objeci)
                arr.push(ob);
              }
          }
          j=0;
          if(arr.length==0)
          {
            arr=[{text:'暂无搜索结果',value:1}]
          }
          resolve(arr);
          arr=[];
        })
      }
      else{
        return new Promise((resolve, reject) => {
          resolve({text:'暂无搜索结果',value:1});
        })

      }
    },
    selectResult: function (e) {
      let m=searchlist[e.detail.index].eventid;
      searchlist=[];
      wx.navigateTo({
        url: '/pages/eventinfo/eventinfo?id='+m
      })

    },
   //摇一摇切换重要性
   switchimp(){
     let that=this;
    wx.startAccelerometer({
        interval: 'game',
        success:function(){
          wx.onAccelerometerChange(function(res){
            if(res.x > 3 || res.y >3 || res.z>3){
              wx.showToast({
                title: '正在调整...',
                icon: 'loading',
                duration: 1000,
              });
               that.paixu();
               wx.stopAccelerometer({
                success: (res) => {
                }
              })
            }
          })
        }
      });
},
 
   //重要性排序
    paixu()
    {
      let that=this;
      let eventList=this.data.eventlist;
      that.setData({
        eventlist:[]
      });
      //重要性排序
        let eventl1=[],eventl2=[];
        for(let i=0;i<eventList.length;i++)
        {
          if(eventList[i].eventimp==1)
          {
            eventl1.push(eventList[i]);
          }
         else{
            eventl2.push(eventList[i]);
          }
        }
       
       that.setData({
         eventlist:eventl1.concat(eventl2)
       }) 
    },
    timepaixu()
    {
      let eventlis=this.data.eventlist;
      that.setData({
        eventlist:[]
      });
      eventlis.sort(function(item1, item2){
        return +(item1.eventtime > item2.eventtime) || +(item1.eventtime === item2.eventtime) - 1;
    }) 
    eventlis.reverse();
    this.setData({
      eventlist:eventlis
    })
    },
    //获取事件id
    goeventinfo(e)
    {
      wx.navigateTo({
        url: '/pages/eventinfo/eventinfo?id='+
        e.currentTarget.dataset.id,
      })
    },
    goevent(){
      wx.navigateTo({
        url: '/pages/modievent/modievent',
      })
     },
    //获取事件
    getevent()
    {
      console.log(app.globalData.openid)
      //app.globalData.openid
      let that=this;
      wx.request({
        url:app.globalData.url+'/eventlist', 
        header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
        data: {
            openid: app.globalData.openid
            //app.globalData.openid
         },
         method: 'post',
         success: function (res) {
              if(res.data.length==0)
              {
                that.setData({
                     eventlength:0
                });
              }
             else{
              let list1=res.data;
              list1.sort(function(item1, item2){
                return +(item1.eventtime > item2.eventtime) || +(item1.eventtime === item2.eventtime) - 1;
            }) 
            list1.reverse();
              that.setData({
                eventlist:list1
              })
             }
         },
         fail:function(err){//请求失败之后要做什么
            console.log('请求失败')
            }  
    })
    },
    onShow:function(){
      this.switchimp();
    }
})